from wtforms.fields import StringField,IntegerField,FileField
from wtforms import Form
from wtforms.validators import Email,EqualTo,Length,InputRequired
from flask_wtf.file import FileAllowed,FileSize
from exts import cache
from flask import request
from models.auth import UserModel
from wtforms import ValidationError

class BaseForm(Form):
  @property
  def messages(self):
    message_list = []
    if self.errors:
      for errors in self.errors.values():
        message_list.extend(errors)
    return message_list


class RegisterForm(BaseForm):
    email=StringField(validators=[Email(message="wrong email format")])
    email_captcha=StringField(validators=[Length(6,6,message='wrong email captcha')])
    username=StringField(validators=[Length(3,20,message='no username')])
    # print(username)
    password1=StringField(validators=[Length(6,6,message='no password')])
    password2=StringField(validators=[EqualTo("password1",message='wrong password')])
    graph_captcha=StringField(validators=[Length(4,4,message='wrong graph captcha')])

    def validate_email(self, field):
        email=field.data
        user=UserModel.query.filter_by(email=email).first()
        if user:
            raise ValidationError(message='user has been registered')

    def validate_email_captcha(self,field):
        email_captcha=field.data
        cache_captcha=cache.get(self.email.data)
        if not cache_captcha or email_captcha!=cache_captcha:
            raise ValidationError(message='email captcha is wrong')

    def validate_graph_captcha(self,field):
        graph_captcha=field.data
        key=request.cookies.get('_image_captcha')
        cache_captcha=cache.get(key)
        if not cache_captcha or cache_captcha.lower()!=graph_captcha.lower():
            raise ValidationError(message='graph captcha is wrong!')

class LoginForm(BaseForm):
    email=StringField(validators=[Email(message='wrong email format')])
    password=StringField(validators=[Length(6,6,message='wrong password format')])
    remember=IntegerField(validators=[InputRequired(message='no remember flag')])

class UploadAvatarForm(BaseForm):
    avatar=FileField(validators=[FileAllowed(['jpg','jpeg','png'],\
                                               message='wrong image format'),\
                                   FileSize(max_size=1024*1024*5,message='filesize must below 5M!')])
class UploadImageForm(BaseForm):
    image=FileField(validators=[FileAllowed(['jpg','jpeg','png'],\
                                               message='wrong image format'),\
                                   FileSize(max_size=1024*1024*5,message='filesize must below 5M!')])

class EditProfileForm(BaseForm):
    signature=StringField(validators=[Length(min=2,max=50,message='个性签名必须在2-50字之间')])

class PublicPostForm(BaseForm):
    title=StringField(validators=[Length(min=3,max=200,message="请传入帖子标题(3-200字符之间)")])
    content=StringField(validators=[InputRequired(message="请填写帖子内容!")])
    board_id=IntegerField(validators=[InputRequired(message="请传入版块ID")])

class CommentForm(BaseForm):
    content = StringField(validators=[InputRequired(message="请填写评价内容!")])
    post_id = IntegerField(validators=[InputRequired(message="请传入帖子ID")])